home *** CD-ROM | disk | FTP | other *** search
- COMMENT.DOC 4 June 1994
- Adrian Urquhart 100126, 1211
-
- This Édocumentation Éaccompanies Éthe ÉDIRECTRY.C Éand ÉDIRECTRY.H
- èsource files. As is usual, the source Écode Éis provided on an "as is"
- èbasis, and I will Énot Ébe Éheld Éresponsible Éfor any damage, however
- ècaused, incurred as a result of the use of this code.
-
- This source Éfile Éprovides Éthree Éfunctions Éwhich Écan Ébe used
- èdirectly within your own Éprograms. ÉYou'll Éneed ÉMicrosoft C version
- è6.00 or higher to compile the Écode, Ébut ÉI've included a small model
- èobject file as well. If Éthere Éis Ésufficient interest I will produce
- èobject files for other memory models.
-
- FUNCTION USAGE
-
- int SearchDisk (const char * Target)
-
- This routine uses recursion Éto Ésearch Éthe current directory and
- èall sub-directories for the Éfile Éspecified Éin Target. Target should
- èeither a unique file name e.g. ÉFRED.DOC. ÉIf Éyou want to be able to
- èuse wild cards, you will need to modify the code. The search will stop
- èat the first occurrence of the ÉTarget Éfile name. It's easy enough to
- èadd code to carry on with the search. ÉThe function returns a 1 if the
- èsearch was successful, otherwise a 0.
-
- Int EmptyDirectory (void)
-
- This routine should carry a Government Health Warning. Starting at
- èthe current directory, it uses recursion Éto empty this directory plus
- èall Ésub-directories, Éremoving ÉÉeach ÉÉsub-directory ÉÉas Éit Égoes.
- èTherefore, if you call it at Éthe Éroot Édirectory of a disk, you will
- èend up with an Éempty Édisk. ÉThe Éonly Éexception Éto Éthis Éis if it
- èencounters a read-only file, in Éwhich Écase Éthe routine ends. Hidden
- èfiles and system files are removed. ÉThe Éfunction Éreturns a 1 if all
- èdirectories could be emptied, otherwise a 0.
-
- Int CreateDirectory (char * NewPath)
-
- This function overcomes the Élimitation Éof Éthe mkdir() function,
- èwhich can only create one Édirectory Élevel Éat Éa time. The parameter
- èNewPathName should include a drive specification, Éand must end with a
- èbackslash i.e. d:\level1\level2\level3\finalone\. It doesn't matter of
- èsome or all of the directory path already exists. The function returns
- èa 1 if the new directory was created, otherwise a 0.
-
- FUNCTION DESCRIPTION
-
- SearchDisk
-
- This function uses a structure of type find_t, which is defined in
- èdos.h. The structure has the following members:
-
- struct find_t
- {
- char reserved [21] ; /* Used by DOS */
- char attrib ; /* Attribute byte of file */
- unsigned wr_time ; /* Time of last file update */
- unsigned wr_date ; /* Date of last file update */
- long size ; /* Length of file, in bytes */
- char name [13] ; /* Null-terminated file name */
- } ;
-
- Firstly, a Écall Éto É_dos_findfirst() Éis Émade, Égiving Éa "*.*"
- èfilespec as the file name to search for. Only normal and sub-directory
- èfiles will be found. The address Éof Éthe find_t structure is given as
- èthe third parameter. If a 0 is Éreturned, Éthen a file has been found,
- èand its details will be in the find_t structure.
-
- We now find out if it's a sub-directory which has been located. If
- èit is, we make sure it's Énot Éthe Éaliases Éfor the current or parent
- èdirectories (i.e. "." and ".."). ÉIf Énot, Éwe change directory to the
- èfound directory, and call SearchDisk again.
-
- If it wasn't a Ésub-directory, Éwe Écompare Éthe Éfile name to the
- èTarget name. If there is an exact match, Éwe return with a 1. If there
- èis no match, we now Éenter Éa Éloop, calling _dos_findnext() to locate
- èthe next file in the directory Éuntil Éthere are no more files. Again,
- èwe check to see if the current Éfile Éis a sub-directory, and if it is
- èwe change directory to it, Éand Écall ÉSearchDisk. ÉIf it's not a sub-
- èdirectory we compare the file name Éto Éthe ÉTarget, and if there is a
- èmatch we return with a 1.
-
- If we have called ÉSearchDisk Éfrom Éwithin Éitself and it returns
- èwith a 0, then we move back Éup Éto Éthe parent directory and carry on
- èwith the search. If a É1 Éis Éreturned, Éwe Éknow Éthat the search was
- èsuccessful, so the function returns. ÉUsing Éthe stricmp() function to
- ècompare the current file with the Étarget Éfile means that the case of
- èthe Target is unimportant.
-
- EmptyDirectory
-
- This function works in much Éthe Ésame Éway as SearchDisk. It uses
- èthe same find_t Éstructure Éand É_dos_findfirst() Écall Éto locate the
- èfirst file in the current Édirectory. ÉThis Étime, however, we specify
- èthat normal, hidden, system, and Ésub-directory files should be found.
- èThereafter, operation is the same as Éfor SearchDisk, except that each
- èfile is deleted Éas Éit Éis Éfound, Éusing Éthe Éremove() function. If
- èEmptyDirectory has been called from within Éitself and it returns with
- èa 1, then that means that Éthe Écurrent directory has been emptied, so
- èwe move back up to the Éparent directory and remove the sub-directory.
- èIf a 0 Éis Éreturned, Éthat Égenerally Émeans Éthat Éthe sub-directory
- ècontains a read-only Éfile, Éwhich Écan't Ébe Éerased. ÉIn Éthis case,
- èEmptyDirectory returns to its caller with a 0 value.
-
- You could modify the code Éso Éthat Éeven Éif a read-only file was
- èencountered, its attribute Écould Ébe Échanged Éand Éthen Éit could be
- èdeleted.
-
- CreateDirectory
-
- This Éroutine Éalso Éuses Éa Ésupport Éroutine, ÉExtractDirectory.
- èFirstly, the NewPath parameter is broken into Éits component parts by
- èthe _splitpath() function. After the Écall, the local variables drive,
- èdir, filename, and ext will hold Éthe drive, directory path name, file
- èname, and file extension, respectively. In our case, the file name and
- èextension are not required.
-
- We then attempt to change the Écurrent Édrive to the one specified
- èin the NewPath argument. If not successful, Éwe return with a 0 value.
- èOtherwise, we move to the root directory of the drive.
-
- Next, we repeatedly Éextract Éeach Édirectory Éname Éfrom Éthe dir
- èvariable, and then try to change directory to it. If we can't, we call
- èmkdir() Éto Écreate Éit, Éand ÉÉthen ÉÉretry Échanging Édirectory. ÉIf
- èsuccessful, we carry on, otherwise we return with a 0 value.
-
- To extract each directory, the local variable path is first set to
- èpoint to the second character of Éthe Édir Évariable. This will be the
- èfirst letter of the directory, after Éthe Éinitial É'\'. A call to the
- èExtractDirectory routine is made, Égiving Épath Éas an argument, along
- èwith the NextDirectory variable, which upon Éreturn will hold the name
- èof the next directory. ÉUpon Éreturn Éfrom ExtractDirectory, path will
- èpoint to the starting position within Éthe dir variable from where the
- ènext directory name is to be extracted.
-
- Within ExtractDirectory, for as long Éas Éparameter É1 points to a
- ènon-zero character which is Énot Éa Ébackslash Éthen that character is
- ècopied to the Ébuffer Épointed Éto Ébe Éparameter É2. ÉParameter É1 is
- èincremented, and the Ébuffer Éindex É(local Évariable Éx) incremented.
- èNext, a 0 is Éappended Éto Éthe Énew Édirectory Éname Éwhich is now in
- èparameter 2. If x is 0, that Émeans that there were no more characters
- èto be extracted from parameter É1, Éso Éa ÉNULL is returned, otherwise
- èparameter 1 is returned.
-
- -------------------------------
-
- Plainly, there is plenty of scope for adding improvements to these
- èfunctions. CreateDirectory could check Éfor Éillegal Édrives or names,
- èfor example.
-